package cn.service;

import cn.dao.UserDao;
import cn.dao.UserDaoImpl;
import cn.dao.user.UserMapper;
import cn.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Transactional
@Service("userService")
public class UserServiceImpl implements UserService{

   /* @Autowired(required = false)
    private UserDao userDao;

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    @Override
    public void saveUser(User user) {
        userDao.saveUser(user);
    }*/
    @Autowired
    private UserMapper userMapper;

    public UserMapper getUserMapper() {
        return userMapper;
    }

    public void setUserMapper(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @Override
    public List<User> getList(User user) {
        try{
            return userMapper.selectList(user);
        }catch (RuntimeException e){
            e.printStackTrace();
            throw e;
        }
    }

    @Override
    public int add(User user) {
        int result = 0;
        try {
            if (userMapper.add(user) > 0){
                result = 1;
            }
        }catch (RuntimeException e){
            e.printStackTrace();
            throw e;
        }
        return result;
    }

    @Override
    public int delete(String id) {
        int result = 0;
        try {
            if (userMapper.delete(id) > 0){
                result = 1;
            }
        }catch (Exception e){
            e.printStackTrace();
            throw e;
        }
        return result;
    }

    @Override
    public int update(User user) {
        int result = 0;
        try {
            if (userMapper.update(user) > 0){
                result = 1;
            }
        }catch (Exception e){
            e.printStackTrace();
            throw e;
        }
        return result;
    }
}